//
//  YD_AnswersView.m
//  SpeedDating
//
//  Created by isu on 17/1/20.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "GifgAnswersView.h"
#import "UIView+SDAutoLayout.h"

@implementation GifgAnswersView{
    NSMutableArray *_buttonsArray;
    NSMutableArray *_BVArr;
}
//-(void)setAnswersArr:(NSArray *)answersArr{
//    _answersArr = answersArr;
//    if (!_buttonsArray) {
//        _buttonsArray = [NSMutableArray new];
//    }
//    
//    int needsToAddItemsCount = (int)(_answersArr.count - _buttonsArray.count);
//    if (needsToAddItemsCount > 0) {
//        for (int i = 0; i < needsToAddItemsCount; i++) {
//            UILabel *ADesLab = [UILabel new];
//            ADesLab.numberOfLines = 0;
//            ADesLab.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
//            [self addSubview:ADesLab];
//            [_buttonsArray addObject:ADesLab];
//            
//        }
//    }
//    
//    NSMutableArray *temp = [NSMutableArray new];
//    [_buttonsArray enumerateObjectsUsingBlock:^(UILabel *lab, NSUInteger idx, BOOL *stop) {
//        if (idx < _answersArr.count) {
//            lab.hidden = NO;
//            lab.text = _answersArr[idx];
//            if(idx!=0){
//                UILabel *fLab = _buttonsArray[idx-1];
//                lab.sd_resetLayout
//                .topSpaceToView(fLab,30)
//                .leftEqualToView(fLab)
//                .rightEqualToView(fLab)
//                .autoHeightRatio(0);
//            }else{
//                lab.sd_resetLayout
//                .leftEqualToView(self)
//                .rightEqualToView(self)
//                .topSpaceToView(self,30)
//                .autoHeightRatio(0);
//            }
//            [temp addObject:lab];
//        } else {
//            [lab sd_clearAutoLayoutSettings];
//            lab.hidden = YES;
//        }
//    }];
//    
//    UILabel *Lab= _buttonsArray[answersArr.count-1];
//    [self setupAutoHeightWithBottomView:Lab bottomMargin:10];
//}

-(void)setAnswersArr:(NSArray *)answersArr{
    _answersArr = answersArr;
    if (!_buttonsArray) {
        _buttonsArray = [NSMutableArray new];
    }
    
    if(!_BVArr){
        _BVArr = [NSMutableArray new];
    }

    int needsToAddItemsCount = (int)(_answersArr.count - _BVArr.count);
    if (needsToAddItemsCount > 0) {
        for (int i = 0; i < needsToAddItemsCount; i++) {
            UIView *v = [UIView new];
            v.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
            [self addSubview:v];
            [_BVArr addObject:v];
            
            UILabel *ADesLab = [UILabel new];
            ADesLab.backgroundColor = [UIColor clearColor];
            [v addSubview:ADesLab];
            [_buttonsArray addObject:ADesLab];
        }
    }

    [_BVArr enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        UILabel *lab = _buttonsArray[idx];
        if (idx < _answersArr.count) {
            lab.hidden = NO;
            lab.text = _answersArr[idx];
            lab.sd_resetLayout
            .topSpaceToView(view,10)
            .leftSpaceToView(view,10)
            .rightSpaceToView(view,10)
            .autoHeightRatio(0);
        } else {
            [lab sd_clearAutoLayoutSettings];
            lab.hidden = YES;
        }

        if (idx < _answersArr.count) {
            view.hidden = NO;
            if(idx!=0){
                UIView *v = _BVArr[idx-1];
                view.sd_resetLayout
                .topSpaceToView(v,10)
                .leftEqualToView(v)
                .rightEqualToView(v);
            }else{
                view.sd_resetLayout
                .leftEqualToView(self)
                .rightEqualToView(self)
                .topSpaceToView(self,10);
            }
        } else {
            [view sd_clearAutoLayoutSettings];
            view.hidden = YES;
        }
        [view setupAutoHeightWithBottomView:lab bottomMargin:10];
    }];
    UIView *v= _BVArr[answersArr.count-1];
    [self setupAutoHeightWithBottomView:v bottomMargin:10];
}

@end
